from flask import Blueprint, jsonify, request
from app.server.ensemble_model import TemperatureForecaster

predict_ensemble_bp = Blueprint('predict_ensemble', __name__)


@predict_ensemble_bp.route('/predict_ensemble', methods=['GET'])
def ensemble_predict():
    days = request.args.get('days', default=7, type=int)
    target = request.args.get('target', default='guilin_max')
    forecaster = TemperatureForecaster(target_col=target)
    try:
        predictions = forecaster.predict(horizon=days)
        return jsonify({
            'status': 'success',
            'requested_days': days,
            'predicted_days': len(predictions['predictions']),
            'predictions': predictions['predictions']
        })
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 500
